package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.GysGroupAddDTO;
import org.jsola.hr.dto.GysGroupUpdateDTO;
import org.jsola.hr.query.GysGroupQuery;
import org.jsola.hr.service.IGysGroupService;
import org.jsola.hr.vo.GysGroupListVO;
import org.jsola.hr.vo.GysGroupVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 甲方端供应商分组
 *
 * @author zhr
 */
@RestController("hrGysGroupController")
@RequestMapping(HrConstants.API_PATH + "/v1/gysGroups")
public class GysGroupController implements IGysGroupControllerAPI {

    @Autowired
    private IGysGroupService gysGroupService;

    @Override
    @PostMapping
    public Result<GysGroupVO> save(@Valid @RequestBody GysGroupAddDTO gysGroupAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增甲方端供应商分组权限校验

        return Result.success(
                gysGroupService.save(gysGroupAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody GysGroupUpdateDTO gysGroupUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改甲方端供应商分组权限校验

        int count = gysGroupService.update(gysGroupUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{gysGroupId}")
    public Result<Integer> delete(@PathVariable String gysGroupId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除甲方端供应商分组权限校验

        int count = gysGroupService.deleteByIds(tokenUser, gysGroupId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] gysGroupIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除甲方端供应商分组权限校验

        int count = gysGroupService.deleteByIds(tokenUser, gysGroupIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{gysGroupId}")
    public Result<GysGroupVO> findById(@PathVariable String gysGroupId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看甲方端供应商分组权限校验

        return Result.success(
                gysGroupService.selectById(gysGroupId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<GysGroupListVO>> listAll(@Valid GysGroupQuery gysGroupQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看甲方端供应商分组权限校验

        return Result.success(
                gysGroupService.select(gysGroupQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<GysGroupListVO>> page(@Valid GysGroupQuery gysGroupQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                gysGroupService.selectPage(gysGroupQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/selectGroupTree")
    public Result<List<GysGroupVO>> selectGroupTree(@Valid GysGroupQuery gysGroupQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                gysGroupService.selectGroupTree(gysGroupQuery, tokenUser)
        );
    }

}

